//
//  NextViewController.m
//  XMLParser
//
//  Created by honeyrr on 2017/10/17.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "NextViewController.h"
#import "GDataXMLNode.h"
#import "ElementObj.h"
#import "NSString+StringContentSize.h"


@interface NextViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) ElementObj *elementObj;
@end

@implementation NextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _elementObj = [[ElementObj alloc]init];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self loadXML];
    
}

#pragma mark 解析XML
- (void)loadXML
{
    NSString *appKey = @"99ADA117C64B87A3AD44FF835CFBD197";
    NSString *word = self.string;
    NSString *wordURL = [NSString stringWithFormat:@"http://dict-co.iciba.com/api/dictionary.php?w=%@&key=%@",word,appKey];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:wordURL]];
    
    GDataXMLDocument *xmlDocument = [[GDataXMLDocument alloc]initWithData:data error:NULL];
    
    NSArray *keys = [xmlDocument nodesForXPath:@"//key" error:NULL];
    for (GDataXMLElement *key in keys) {
        _elementObj.key = [key stringValue];
    }
    //ps
    NSArray *pss = [xmlDocument nodesForXPath:@"//ps" error:NULL];
    for (GDataXMLElement *ps in pss) {
        [_elementObj.ps addObject:ps.stringValue];
    }
    
    NSArray *prons = [xmlDocument nodesForXPath:@"//pron" error:NULL];
    for (GDataXMLElement *pron in prons) {
        [_elementObj.pron addObject:pron.stringValue];
    }
    //pos
    NSArray *poss = [xmlDocument nodesForXPath:@"//pos" error:NULL];
    for (GDataXMLElement *pos in poss) {
        [_elementObj.pos addObject:pos.stringValue];
    }
    //acceptation
    NSArray *acceptations = [xmlDocument nodesForXPath:@"//acceptation" error:NULL];
    for (GDataXMLElement *acceptation in acceptations) {
        [_elementObj.acceptation addObject:acceptation.stringValue];
    }
    
    NSArray *origsArr = [xmlDocument nodesForXPath:@"//orig" error:NULL];
    for (GDataXMLElement *orig in origsArr) {
        NSString *org = [[[orig stringValue] componentsSeparatedByString:@"\n"] objectAtIndex:1];
        [_elementObj.orig addObject:org];
    }
    //trans
    NSArray *transArr = [xmlDocument nodesForXPath:@"//trans" error:NULL];
    for (GDataXMLElement *tran in transArr) {
        NSString *tra = [[[tran stringValue] componentsSeparatedByString:@"\n"] objectAtIndex:1];
        [_elementObj.trans addObject:tra];
    }
    [self.tableView reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count;
    switch (section) {
        case 0:
            count = 1;
            break;
        case 1:
            count = _elementObj.ps.count;
            break;
        case 2:
            count = _elementObj.pos.count;
            break;
        case 3:
            count = _elementObj.orig.count;
            break;
        default:
            count = 0;
            break;
    }
    return count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    cell.textLabel.numberOfLines = 0;
    cell.detailTextLabel.numberOfLines = 0;
    switch (indexPath.section) {
        case 0:
            cell.textLabel.text = _elementObj.key;
            cell.textLabel.font = [UIFont italicSystemFontOfSize:40];
            cell.detailTextLabel.text = @"";
            break;
        case 1:
            if(indexPath.row == 0){
                cell.textLabel.text = [NSString stringWithFormat:@"英 [%@]",_elementObj.ps[indexPath.row]];
            } else {
                cell.textLabel.text = [NSString stringWithFormat:@"美 [%@]",_elementObj.ps[indexPath.row]];
            }
            cell.detailTextLabel.text = @"";
            //cell.detailTextLabel.text =[NSString stringWithFormat:@" [%@]",_elementObj.pron[indexPath.row]];
            break;
        case 2:
            cell.textLabel.text = [NSString stringWithFormat:@"%@  %@",_elementObj.pos[indexPath.row], _elementObj.acceptation[indexPath.row]];
            cell.detailTextLabel.text = @"";
            break;
        case 3:
            cell.textLabel.text = [NSString stringWithFormat:@"%ld. %@",indexPath.row + 1,_elementObj.orig[indexPath.row]];
            cell.detailTextLabel.text = _elementObj.trans[indexPath.row];
            cell.textLabel.font = [UIFont systemFontOfSize:16];
            break;
        default:
            break;
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 3) {
        CGSize sizeOrig = [NSString sizeWithString:_elementObj.orig[indexPath.row] maxSize:CGSizeMake(self.tableView.frame.size.width, MAXFLOAT) font:[UIFont italicSystemFontOfSize:20]];
        CGSize sizeTrans = [NSString sizeWithString:_elementObj.trans[indexPath.row] maxSize:CGSizeMake(self.tableView.frame.size.width, MAXFLOAT) font:[UIFont italicSystemFontOfSize:20]];
        return sizeOrig.height + sizeTrans.height;
    } else if (indexPath.section == 2){
        NSString *pos = [NSString stringWithFormat:@"%@  %@",_elementObj.pos[indexPath.row], _elementObj.acceptation[indexPath.row]];
        CGSize size = [NSString sizeWithString:pos maxSize:CGSizeMake(self.tableView.frame.size.width, MAXFLOAT) font:[UIFont italicSystemFontOfSize:20]];
        return size.height;
    } else if (indexPath.section == 1){
        CGSize sizePs = [NSString sizeWithString:_elementObj.ps[indexPath.row] maxSize:CGSizeMake(self.tableView.frame.size.width, MAXFLOAT) font:[UIFont italicSystemFontOfSize:20]];
        //CGSize sizePron = [NSString sizeWithString:_elementObj.pron[indexPath.row] maxSize:CGSizeMake(self.tableView.frame.size.width, MAXFLOAT) font:[UIFont italicSystemFontOfSize:20]];
        return sizePs.height + 10;
    } else {
        CGSize sizeKey = [NSString sizeWithString:_elementObj.key maxSize:CGSizeMake(self.tableView.frame.size.width, MAXFLOAT) font:[UIFont italicSystemFontOfSize:40]];
        return sizeKey.height;
    }
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 1) {
        return @"发音：";
    } else if(section == 2) {
        return @"释义：";
    } else if(section == 3) {
        return @"双语例句：";
    } else {
        return @"单词";
    }
}

@end
